package de.hsb.ai.learning;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import weka.associations.Apriori;
import weka.associations.AprioriItemSet;
import weka.core.FastVector;
import weka.core.Instances;
import de.hsb.ai.util.ScotlandJadeHelper;

public class RuleDetector {
	
	public static final Logger logger = LoggerFactory.getLogger("sy.rulz");
	
	public static void main(String[] args) {
		Instances data = FugitiveAnalysis.load(ScotlandJadeHelper.createGameLogger().read().iterator());
		Apriori aa = new Apriori();
		
		for (int i = 0; i < data.numAttributes(); ++i) {
			if (!data.attribute(i).isNominal()) {
				logger.warn("deleting attribute " + data.attribute(i).name());
				data.deleteAttributeAt(i);
				--i;
			}
		}
		
		try {
			aa.buildAssociations(data);
			FastVector[] rulz = aa.getAllTheRules();
			for (FastVector rule : rulz) {
				System.out.println("---> " + rule);
				if (rule == null) continue;
				for (int i = 0; i < rule.size(); ++i) {
					Object o = rule.elementAt(i);
					if (o instanceof AprioriItemSet) {
//						((AprioriItemSet) o).
						logger.info(((AprioriItemSet) o).toString(data));
					} else {
						logger.info("rule: " + o);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
